﻿(function($) {
    jQuery.fn.extend({
        UserLoginAjax: function(opts) {
            opts = jQuery.extend({
                Un: "#UserName",
                Pwd: "#Pwd",
                Code: "#CodeString",
                LoadImg: "#imgloading"
            }, opts || {});

            var _this = $(this);
            var _Un = $(opts.Un); // $.trim(.val()); //用户名
            var _Pwd = $(opts.Pwd); //$.trim($(opts.Pwd).val()); //密码
            var _Code = $(opts.Code); // $.trim($(opts.code).val()); //验证码
            var IsCode = false; //是否支持验证码
            var _LoadImg = $(opts.LoadImg);
            //改变按钮的CSS样式
            _this.css({ cursor: "hand" });
            _this.click(function() {
                if ($.trim(_Un.val()) + "" == "") {
                    alert("用户名不能为空!");
                    $(_Un).focus();
                    return false;
                }
                if ($.trim(_Pwd.val()) + "" == "") {
                    alert("密码不能为空!");
                    _Pwd.focus();
                    return false;
                }
                if ($.trim(_Code.val()) + "" == "") {
                    alert("验证码不能为空!");
                    _Code.focus();
                    return false;
                }

                //                $("body").keydown(function() {
                //                    if (event.keyCode == 13) {
                //                        _this.click();
                //                    }
                //                });



                //alert($("#imgloading").html());
                //继续这里
                $.ajax({
                    url: JsPath + "AjaxXml/AjaxUserCheck.aspx",
                    type: 'POST',
                    data: 'Un=' + escape(_Un.val()) + '&Pwd=' + escape(_Pwd.val()) + '&Code=' + escape(_Code.val()),
                    dataType: "json",
                    beforeSend: function() {
                        _LoadImg.css({ "display": "block" })
                        _this.attr("disabled", "disabled");
                    },
                    error: function(data) {
                        alert("登录失败");
                        return false;
                    },
                    success: function(data) {
                        _LoadImg.css("display", "none");
                        var re = data.result;
                        if (re == "-1") {
                            alert(data.msg);
                            return false;
                        }
                        else if (re == "-2") {
                            alert(data.msg);
                            $('#UsercheckLoginCode').attr("src", JsPath + 'CheckCode.Aspx?id=' + Math.random());
                            _Code.select();
                            return false;
                        }
                        else if (re >= 1) {
                            location.href = data.msg;
                            return true;
                        }
                        else {
                            alert(data.msg);
                            _Un.select();
                            return false;
                        }
                    },
                    complete: function(data, textStatus) {
                        _LoadImg.css("display", "none");
                        _this.removeAttr("disabled");

                    }
                });

            });
        }







    });
})(jQuery);